package com.cwj.mansysback.dao.impl;

import com.cwj.mansysback.dao.BaseDao;
import com.cwj.mansysback.dao.api.CourseDao;
import com.cwj.mansysback.entity.Course;

import java.util.List;

public class CourseDaoImpl extends BaseDao<Course> implements CourseDao {

    private  final  static String TABLE_NAME = "course";

    @Override
    public boolean deleteCourse(String cid) {
        String sql = "delete from "+CourseDaoImpl.TABLE_NAME+"\twhere cid = ?";

        return super.update(sql,cid) > 0;
    }

    @Override
    public Course selectCourse(String cid) {

        String sql = "select * from\t"+CourseDaoImpl.TABLE_NAME+"\twhere cid = ?";

        return super.select(Course.class,sql,cid);
    }

    @Override
    public boolean updateCourse(String cid, String cname, String tid, Integer credit) {

        String sql = "update "+CourseDaoImpl.TABLE_NAME+"\t set cname = ?,tid = ? , credit = ?  where cid = ?";

        return super.update(sql,cname,tid,credit,cid) > 0;
    }

    @Override
    public List<Course> selectCourseTea(String tid) {
        String sql = "select * from "+CourseDaoImpl.TABLE_NAME+"\twhere tid = ? ";
        System.out.println(sql);
        return super.selectBatch(Course.class,sql,tid);
    }

    @Override
    public boolean insertCourse(String cid, String cname, String tid,Integer credit) {
        String sql ="insert into "+CourseDaoImpl.TABLE_NAME+"\t(cid,cname,tid,credit) values (?,?,?,?)";
        return super.update(sql,cid,cname,tid,credit) > 0;
    }

    @Override
    public List<Course> selectSomeCourses(Integer pageInd, Integer rowCount, String orderKey, boolean isAsc) {

        String sql = "select c.*,tt.* from course as c left join (\n" +
                "select  cid as id,count(1) as `count`, avg(cscore) as avgScore,min(cscore) as minScore,max(cscore) as maxScore \n" +
                "from electives AS e   left outer join \n" +
                "student AS st  on st.`sid`=e.`sid` \n" +
                "group by cid ) as tt on c.`cid` = tt.id order by ? "+(isAsc?"ASC":"DESC")+" limit ?,?";

        return super.selectBatch(Course.class,sql,orderKey,(pageInd-1)*rowCount,rowCount);
    }
}
